
<script>
    layui.use(['layer', 'form','upload','laytpl'], function(){
        var upload = layui.upload;
        var layer = layui.layer;
        var form = layui.form;
        var laytpl = layui.laytpl;

        //创建一个上传组件
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/admin/api/upload'
            ,accept:'images'
            ,acceptMime: 'image/*'
            ,field:'photo'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    // $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败',{icon:2});
                }else{
                    let coverTpl = demo.innerHTML;
                    laytpl(coverTpl).render(res.data, function(html){
                        $('#test1').html(html);
                    });
                    return layer.msg('上传完成',{icon:1});
                }
                //上传成功
            }
        });

        let tip = false;
        function tips() {
            if (tip) {
                return false;
            }
            tip = true;
            layui.use(['layer'],function () {
                let layer = layui.layer;
                layer.tips('点击可以更换图片', '#test1', {
                    tips: 1
                });
                setTimeout(function () {
                    tip = false;
                },5000)
            });
        }

        @component('admin.ajax')

        @endcomponent

    });
</script>

<script id="demo" type="text/html">
    <img class="layui-upload-img" style="width: 100%;height: 100%;" src="@{{d.url}}" id="demo1">
    <input type="hidden" value="@{{d.url}}" name="cover">
</script>